from PIL import Image
import requests
import io
from app.libs.qrcode import gzh_qrcode
import os
 
class PosterGenerator():
    
    def unlockAIGCPoster(self, order_number):
        if not os.path.exists(f'/mnt/unlock_poster/unlock_aigc_{order_number}.png'):
            poster = Image.open('/home/SameReportApi/images/gzh_poster.jpg')
            qrimg = self.qrcode(order_number)
            new_size = (165, 165)  # 设定新的尺寸为 100x100 像素
            qrimg = qrimg.resize(new_size)  # 调整 overlay 大小
            poster.paste(qrimg, (365,389))
            poster.save(f'/mnt/unlock_poster/unlock_aigc_{order_number}.png')
        
    
    def qrcode(self,order_number):
        qrmodel = gzh_qrcode()
        ticket = qrmodel.unlock_aigc_qrcode_ticket(order_number)
        qrurl = f'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={ticket}'
        response = requests.get(qrurl)
        if response.status_code == 200:
            image = Image.open(io.BytesIO(response.content))
            print(image)
            return image
        else:
            return ''
 


if __name__ == '__main__':
    bot = PosterGenerator()
    bot.qrcode()